﻿using System;
using System.Collections.Generic;
using System.Web;
using CTS.Com.Domain.Helper;
using CTS.Com.Domain.Logic;
using CTS.Com.Domain.Model;
using CTS.Web.Com.Domain.Helper;

namespace CTS.Web.Com.Domain.Utils
{
    public class AjaxCom
    {
        public void Refresh()
        {
            Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
        }

        public void Redirect(string url)
        {
            HttpContext.Current.Response.Redirect(url);
        }

        public void RedirectByReturnUrl(string url)
        {
            if (!DataCheckHelper.IsNull(HttpContext.Current.Request["returnUrl"])) {
                url = HttpContext.Current.Request["returnUrl"];
            }
            Redirect(url);
        }

        public T Invoke<T>(IOperateLogic<T> logic, object param) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param);
        }

        public T Invoke<T>(IOperateLogic<T> logic, object param, Type paramType) where T : BasicResponse, new()
        {
            return PageHelper.Invoke<T>(logic, param, paramType);
        }

        public T CopyValue<T>(object data) where T : new()
        {
            return DataHelper.CopyObject<T>(data);
        }

        public bool IsNull(string data)
        {
            return DataCheckHelper.IsNull(data);
        }

        public object GetSession(string name)
        {
            return HttpContext.Current.Session[name];
        }

        public decimal ToDecimal(string data)
        {
            var value = DataHelper.ConvertInputNumber(data);
            return Convert.ToDecimal(value);
        }

        public decimal ToDecimal(decimal? data)
        {
            return Convert.ToDecimal(data);
        }

        public string ToString(bool? data)
        {
            return DataHelper.ToString(data);
        }

        public string ToString(object data)
        {
            return Convert.ToString(data);
        }

        public string ToSubString(object data, int size)
        {
            var strData = Convert.ToString(data);
            return ToSubString(strData, size);
        }

        public string ToSubString(string data, int size)
        {
            return DataHelper.ToSubString(data, size);
        }

        public IList<object> ToList(object data)
        {
            return new List<object>() { data };
        }

        public ComboItem ToComboItem(object data)
        {
            return DataHelper.ToComboItem(data);
        }

        public string Format(string format, params object[] args)
        {
            return string.Format(format, args);
        }
    }
}